package cn.itcast.dao;

import cn.itcast.bean.UmPassword;
import cn.itcast.bean.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ListMessge {
    public void listUm(UmPassword i){
        try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection conn =  DriverManager.getConnection("jdbc:mysql://localhost:3306/db5","root","");

            PreparedStatement preparedStatement = conn.prepareStatement("update   t_nump  set password=? where uid=?");
            preparedStatement.setString(1,i.getPassword());
            preparedStatement.setString(2, i.getUid());

            preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public   void  regUm(UmPassword i){
        try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection conn =  DriverManager.getConnection("jdbc:mysql://localhost:3306/db5","root","");

            PreparedStatement preparedStatement = conn.prepareStatement("insert  into  t_nump  values(null,?,?)");

            preparedStatement.setString(1, i.getUid());
            preparedStatement.setString(2,i.getPassword());
            preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public List<User> findAll() {
        try {
            List<User>  users = new ArrayList<>();
            Class.forName("com.mysql.jdbc.Driver");

            Connection conn =  DriverManager.getConnection("jdbc:mysql://localhost:3306/db5","root","");

            PreparedStatement preparedStatement = conn.prepareStatement("select * from  t_nump");

            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setName(resultSet.getString("uid"));
                user.setAge(resultSet.getInt("password"));
                users.add(user);
            }

            return   users;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }
}
